#coding:utf8
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from pynput import keyboard
import time
from pynput import mouse
import sys,json,os
import functools

class my77(QMainWindow):
    def __init__(self,screen_config,indx,is_type2,has_r_win,has_win,not_show=False):
        super().__init__()               
        self.is_type2 = is_type2
        self.indx = indx
        self.screen_config = screen_config
        self.has_r_win = has_r_win
        self.has_win = has_win
        self.xfirst = True
        if has_win:
            self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint)        
        else:
            self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.ToolTip) 
            
        self.setAttribute(Qt.WA_TranslucentBackground)     
        if not not_show:
            self.show()        
        self.last_ge = None
        self.l = []
        self.l2 = []
        self.last_scr = None
        self.co = 0
        t = QTimer(self)
        t.timeout.connect(self.ont)
        t.start(10)
        self.is_min = False        
        self.is_min_ex = False
    def set_xfirst(self,b):
        if self.xfirst!=b:
            self.xfirst=b
            self.update()
            
    def ont(self):
        if self.is_min == self.is_min_ex:
            return
        self.is_min = self.is_min_ex
        print('ont',self.is_min)
        x,y = mouse.Controller().position
        screen_indx = self.get_screen_indx(x, y)        
        conf = self.screen_config[screen_indx] 
        self.move_win(conf)
        if self.is_min:
            self.dohide()
        else:
            self.doshow()
        
    def dohide(self):
        if not self.has_win:
            print('77 hide')
            self.hide()
        else:
            self.showMinimized()
            
    def doshow(self):
        if not self.has_win:
            print('77 show')
            self.show()
        else:
            self.showMaximized()  
            
    def get_screen_indx(self,x,y):
        for i in range(len(self.screen_config)):
            con = self.screen_config[i]
            if x>=con[0] and y>=con[1] and x<=con[0]+con[2] and y<= con[1]+con[3]:
                return i
        return 0      
               
    def cal_pos2(self):        
        l = []        
        lsign = '左'
        rsign = '右'        
        for i in range(1,13):
            l.append('f%s'%i)
        l.append('~')
        s = '1234567890-+'
        for i in s:
            l.append(i)
        l.append('退格')
        l.append('tab')
        s = 'QWERTYUIOP[]'               
        for i in s:
            l.append(i)
        if self.is_type2:
            l.append('回车')
        else:            
            l.append('\\')        
            
        self.l = l    
        l = []
        s = 'ASDFGHJKL;"'        
        for i in s:
            l.append(i)
        if self.is_type2:
            l.append('\\')        
        else:            
            l.append('回车')        
        l.append(lsign+'⇧')
        s = 'ZXCVBNM<>?'
        for i in s:
            l.append(i)
        l.append(rsign+ '⇧')
        l.append(lsign+'^')
        l.append(lsign+'Win')
        l.append(lsign+'Alt')
        l.append('空格')
        l.append(rsign+'Alt')
        if self.has_r_win:
            l.append(rsign+'Win')
        l.append('菜单')
        l.append(rsign+'^')
        self.l2 = l
        
    def cal_pos8(self):                         
        s1='~1234567890-'
        s2='→QWERTYUIOP['        
        s3 = '↑ASDFGHJKL;"'
        s4='⇧ZXCVBNM<>/⇧'                
        self.l = [s1,s2,s3,s4]        
        
    def cal_pos(self):        
        l = []
        lsign = '左'
        rsign = '右'        
            
        for i in range(1,13):
            l.append('f%s'%i)
        l.append('~')
        s = '1234567890-+'
        for i in s:
            l.append(i)
        l.append('退格')
        l.append('tab')
        s = 'QWERTYUIOP[]'
        for i in s:
            l.append(i)
        if self.is_type2:
            l.append('回车')
        else:            
            l.append('\\')  
            
        s = 'ASDFGHJKL;"'        
        for i in s:
            l.append(i)
        if self.is_type2:
            l.append('\\')        
        else:            
            l.append('回车')     
        l.append(lsign+'⇧')
        s = 'ZXCVBNM<>?'
        for i in s:
            l.append(i)
        l.append(rsign+ '⇧')
        l.append(lsign+'^')
        l.append(lsign+'⌘')
        l.append(lsign+'Alt')
        l.append('空格')
        l.append(rsign+'Alt')
        if self.has_r_win:
            l.append(rsign+'⌘')
        l.append('菜单')
        l.append(rsign+'^')
      
        l2 = []
        for i in l:            
            l2.append(i)
            
        if self.indx in (1,3):
            self.l = l2
        else:
            self.l = []
              
        l2 = []
        for i in l:            
            l2.append(i)
            
        if self.indx in (1,4):
            self.l2 = l2
        else:
            self.l2 = []                            
    
    def move_win(self,conf):
        self.setGeometry(conf[0]-2,conf[1]-2,conf[2]-2,conf[3]-2)
       
    def paintEvent(self,e):
        self.co += 1
        print('paintEvent1',self.co,time.monotonic())
        
        x,y = mouse.Controller().position
       
        screen_indx = self.get_screen_indx(x, y)        
        conf = self.screen_config[screen_indx]
        if self.indx in ( 1,3,4):
            self.cal_pos()
        elif self.indx==2:
            self.cal_pos2()
        elif self.indx==8:
            self.cal_pos8()
        print('paintEvent2',self.co,time.monotonic())
        painter = QPainter (self);
        opt = QStyleOption()
        opt.initFrom(self);
        self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self);
        w = conf[2]
        h = conf[3]
                
        def draw_txt(txt,x,y,color=1):
            y = y+10
            if txt=='':
                return
            if self.indx in (2,8):
                color=1
            if color!=1:
                txt = '?'
            path = QPainterPath()
            font = QFont()
            font.setBold(True);        
            font.setFamily("Arial");
            font.setPixelSize(16);            
            path.addText(x, y, font, txt);
        
            pen = QPen()
            #pen.setColor(Qt.white);
            
            if color==1:
                pen.setColor(Qt.white);
            else:
                pen.setColor(QColor(255,255,255));
                
            pen.setStyle(Qt.SolidLine);
            pen.setWidth(6);    
            
            painter.setRenderHint(QPainter.Antialiasing);
                           
            painter.strokePath(path, pen);
           
            #painter.fillPath(path, QBrush(Qt.black));  
            
            if color==1:
                painter.fillPath(path, QBrush(Qt.black));  
            else:
                painter.fillPath(path, QBrush(QColor(0,0,0)));  
                
        def draw_x(color):
            l = self.l
            gap = 2
            if self.indx in (2,8):
                gap = 1
            for i in range(len(l)):   
                if i%gap==0:                
                    dx = i/(len(l)-1)
                    draw_txt(l[i],int(dx*w),20,color)                                
                    draw_txt(l[i],int(dx*w),h-60,color)   
        def draw_y(color):        
            l = self.l2
            gap = 2
            if self.indx in (2,8):
                gap = 1            
            for i in range(len(l)):                  
                if i%gap==0:                
                    dx = i/(len(l)-1)
                    draw_txt(self.l2[i],20,int(dx*h),color)
                    draw_txt(self.l2[i],w-60,int(dx*h),color)
        if self.indx==8:
            l = self.l            
            for i in range(len(l)):   
                for j in range(len(l[0])):                  
                    dy = (i)/(len(l))
                    dx = (j)/(len(l[0]))
                    draw_txt(l[i][j],max(10,int(dx*w)),max(20,int(dy*h)))      
                    
                    
        else:
            if self.xfirst:
                draw_y(2)
                draw_x(1)
                
            else:
                draw_x(2)
                draw_y(1)
            
            
if __name__ == '__main__':
    def toggk():
        w2.is_min_ex = not w2.is_min_ex          
        print('toggk over')
    is_type2 = True
    has_r_win = False
    has_win = True
    not_show = False
    screen_config = [(0,0,1366,768),(-2560,0,2560,1440)]
    hotkeys = keyboard.GlobalHotKeys({      
     '<ctrl>+<alt>+h': toggk})
    hotkeys.start()        
    app = QApplication([])   
    ty = 1 # 1,2,8
    w2 = my77(screen_config,2,is_type2,has_r_win,has_win,not_show)
    toggk()
    app.exec_()
